import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:niuren_demo/configs/App_style_config.dart';
import 'package:niuren_demo/model/TeamPage_model.dart';
import 'package:niuren_demo/view/dynamic_page/TeamItem.dart';
import 'package:niuren_demo/view/dynamic_page/jk_over_scroll_behavior.dart';
import 'package:niuren_demo/view/text/text_style.dart';

class TeamPage extends StatefulWidget {
  @override
  _TeamPageState createState() => _TeamPageState();
}

class _TeamPageState extends State<TeamPage> {
  TeamPageDataModel dataModel ;

  void initState() {
    super.initState();
    loadAsset();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xffF5F5F5),
      child:ScrollConfiguration(
        behavior: JKOverScrollBehavior(),
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            if (index == 0) return buildTeamTop();
            if (index == 1)return buildTeamItemTitle();
            return TeamItem(dataMode: dataModel?.temaNembers[index - 2],);
          },
          itemCount: (dataModel?.temaNembers?.length?? 0) + 2,
        ),
      ),
    );
  }

  Widget buildTeamTop(){
    return Container(
      margin: EdgeInsets.only(bottom: 8),
      color: Colors.white,
      child: Column(
        children: [
          buildTeamTopRow(
            children: [
              buildTeamTopRowItem(title: "团队", amount: dataModel?.tema?.total??"0"),
              buildTeamTopRowItem(title: "直接", amount:  dataModel?.tema?.directly??"0"),
              buildTeamTopRowItem(title: "间接", amount:  dataModel?.tema?.indirect??"0"),
            ],
          ),
          buildTeamTopRow(
            children: [
              buildTeamTopRowItem(title: "分成", amount: dataModel?.divideTnto?.total??"0.00"),
              buildTeamTopRowItem(title: "今日", amount: dataModel?.divideTnto?.today??"0.00"),
              buildTeamTopRowItem(title: "昨日", amount: dataModel?.divideTnto?.yestday??"0.00"),
            ],
          ),
          // 跟单
          buildTeamTopRow(
            children: [
              buildTeamTopRowItem(title: "跟单", amount: dataModel?.fllowe?.total??"0.00"),
              buildTeamTopRowItem(title: "今日", amount: dataModel?.fllowe?.today??"0.00"),
              buildTeamTopRowItem(title: "昨日", amount: dataModel?.fllowe?.yestday??"0.00"),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildTeamTopRow({List<Widget> children}) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 8),
      child: Row(children: children),
    );
  }

  Widget buildTeamTopRowItem({@required String title, String amount}) {
    return Expanded(
      flex: 1,
      child: Column(
        children: [
          Text_style(
            data: title,
            color: Colors.grey,
            fontSize: 12,
          ),
          SizedBox(
            height: 4,
          ),
          Text_style(
            data: amount,
            color: Colors.black,
            fontSize: 16,
            weight: FontWeight.bold,
          )
        ],
      ),
    );
  }

  Widget buildTeamItemTitle(){
    return Container(
      color: Colors.white,
      height: 40,
      width: double.infinity,
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            "团队成员",
            style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
          ),
          Icon(
            Icons.qr_code,
            color: Colors.black,
            size: 20,
          ),
        ],
      ),
    );
  }
  loadAsset() async {
    var value = await rootBundle.loadString('assets/mocks/mock_team_page_data.json');
    Map data = json.decode(value);
    dataModel = TeamPageDataModel.fromJsonMap(data);
    if (!mounted) {
      return;
    }
    setState(() {
    });
  }
}

